#include <stdio.h>
#include <string.h>

#include "BSP/USART/bsp_usart.h"

#if 1
#include <stdio.h>
#pragma import(__use_no_semihosting)

struct __FILE {
  int handle;
};

FILE __stdout;
// define _sys_exit()
void _sys_exit(int x) { x = x; }

/**
 * _ttywrch
 *
 * Error: L6915E: Library reports error: __use_no_semihosting was requested, but _ttywrch was referenced
 *
 * This error occurs because although you have disabled the half-host mode,
 * some functions in the standard library still require the _ttywrch function.
 * When using memory allocation functions such as malloc,
 * the error handling mechanism may be triggered, thereby calling _ttywrch.
 */
void _ttywrch(int ch) {}

#endif

int fputc(int ch, FILE *f) {
  
  while ((USART1->SR & (1 << 6)) == 0);

  USART1->DR = (uint8_t)ch;

  return ch;
}

void Uart_Putchar(uint8_t ch) {

  while (!(USART1->SR & (1 << 6)));

  USART1->DR = ch;
}

uint8_t Uart_Getchar(void) {

  while (!(USART1->SR & (1 << 5)));

  return USART1->DR;
}
